# coding:utf-8
"""
    Author: walrus
    Date: 2024/11/7
    File: log_settings.py
"""
import logging

def set_log_config(my_level = logging.INFO):
    log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    date_format = '%m/%d/%Y %H:%M:%S'
    formatter = logging.Formatter(fmt=log_format, datefmt=date_format)

    logger = logging.getLogger()
    logger.setLevel(my_level)
    # 文件处理器，把所有日志信息输出到文件
    """
    文件路径最好不用绝对路径，文件夹不存在会报错
    """
    file_handler = logging.FileHandler('logs/info.log', mode = 'a', encoding = 'utf-8')
    logger.addHandler(file_handler)
    file_handler.setFormatter(formatter)

    # 流处理器，信息输出到控制台中
    stream_handler = logging.StreamHandler()
    logger.addHandler(stream_handler)
    stream_handler.setFormatter(formatter)

    # 错误日志输出到错误文件中
    error_handler = logging.FileHandler('logs/error.log', mode = 'a', encoding = 'utf-8')
    error_handler.setLevel(logging.ERROR)
    logger.addHandler(error_handler)
    error_handler.setFormatter(formatter)

